//
//  NetworkTools.m
//  susencloud
//
//  Created by tanchao on 15/11/9.
//  Copyright © 2015年 chaoren. All rights reserved.
//

#import "NetworkTools.h"
#import "Account.h"
@implementation NetworkTools
/// 单例
+ (instancetype)sharedTools{
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
         NSURL *baseUrl = [NSURL URLWithString:@"https://e.surdoc.net/"];
        instance = [[self alloc]initWithBaseURL:baseUrl];
        AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
        responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain",@"application/xhtml+xml",@"application/xml", nil];
        [instance setResponseSerializer:responseSerializer];
    });
    return instance;
}
- (void)loginWithEmail:(NSString *)email andPassword:(NSString *)password{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:password forKey:@"password"];
    [[NetworkTools sharedTools]POST:@"client/login" parameters:parameters success:^(NSURLSessionDataTask *task,id responseObject) {
        NSError *err;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject
                                                            options:NSJSONReadingMutableContainers
                                                              error:&err];
        NSLog(@"%@",dic);
        if(err) {
            NSLog(@"json解析失败：%@",err);
        }else{
            [Account loginAccountWithDict:dic];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"failure======>%@",error);
    }];
}

- (void)getfolders{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[Account sharedAccount].access_token forKey:@"Authorization"];
    NSAssert([Account sharedAccount].access_token!=nil, @"token为空！！");
    [[NetworkTools sharedTools]GET:@"/api/v1/folders/0/items" parameters:dict success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@",error);
    }];
}
@end
